package com.ruoyi.web.controller.repository;

import java.util.List;

import com.ruoyi.system.repository.domain.User;
import com.ruoyi.system.repository.service.IUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会员Controller
 *
 * @author qiang
 * @date 2021-01-01
 */
@RestController
@RequestMapping("/repository/user")
public class UserController extends BaseController {
    @Autowired
    private IUserService userService;

/**
 * 查询会员列表
 */
@PreAuthorize("@ss.hasPermi('repository:user:list')")
@GetMapping("/list")
        public TableDataInfo list(User user) {
        startPage();
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 导出会员列表
     */
    @PreAuthorize("@ss.hasPermi('repository:user:export')")
    @Log(title = "会员", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(User user) {
        List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User. class);
        return util.exportExcel(list, "user");
    }

    /**
     * 获取会员详细信息
     */
    @PreAuthorize("@ss.hasPermi('repository:user:query')")
    @GetMapping(value = "/{phone}")
    public AjaxResult getInfo(@PathVariable("phone") String phone) {
        return AjaxResult.success(userService.selectUserById(phone));
    }

    /**
     * 新增会员
     */
    @PreAuthorize("@ss.hasPermi('repository:user:add')")
    @Log(title = "会员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody User user) {
        return toAjax(userService.insertUser(user));
    }

    /**
     * 修改会员
     */
    @PreAuthorize("@ss.hasPermi('repository:user:edit')")
    @Log(title = "会员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody User user) {
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除会员
     */
    @PreAuthorize("@ss.hasPermi('repository:user:remove')")
    @Log(title = "会员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{phones}")
    public AjaxResult remove(@PathVariable String[] phones) {
        return toAjax(userService.deleteUserByIds(phones));
    }
}
